package com.mlethe.android.demo.recyclerview.adapter;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.mlethe.android.demo.recyclerview.adapter.callback.EmptyCallback;
import com.mlethe.android.demo.recyclerview.adapter.callback.ErrorCallback;
import com.mlethe.android.demo.recyclerview.adapter.callback.FooterLoadCallback2;
import com.mlethe.demo.recyclerview.adapter.R;
import com.mlethe.library.recyclerview.callback.ItemDragAndSwipeCallback;
import com.mlethe.library.recyclerview.decoration.GridItemDecoration;
import com.mlethe.library.recyclerview.listener.ItemDecorationListener;
import com.mlethe.library.recyclerview.listener.OnLoadListener;

import java.util.ArrayList;
import java.util.List;

public class HeaderAndFooterActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerViewAdapter adapter = new RecyclerViewAdapter();
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_adapter_header_and_footer);
        recyclerView = findViewById(R.id.recyclerView);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
//        PagerGridLayoutManager layoutManager = new PagerGridLayoutManager(this, 3, 3);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter.enableDragItem()
                .enableSwipeItem()
                .setHeaderFooterEmpty(false, false)
//                .setEmptyWidthAndHeight(RecyclerView.LayoutParams.MATCH_PARENT, dip2px(300))
                .isUseEmpty(true)
                .setEnableLoadMore(true)
                .setLoadCallback(FooterLoadCallback2.class)
                .setOnLoadListener(new OnLoadListener() {
                    int count = 0;

                    @Override
                    public void onLoad() {
                        count++;
                        Log.e("TAG", "onLoad: " + count);
                        if (count % 2 == 0) {
                            loadMore(1);
                        } else {
                            loadMore(2);
                        }
                    }

                    @Override
                    public void onReload() {
                        Log.e("TAG", "onReload: " + count);
                        if (count >= 1) {
                            loadMore(3);
                        } else {
                            loadMore(1);
                        }
                    }
                });
        /*adapter.setOnItemClickListener((v, item, position) -> {
            Log.e("TAG", "onCreate: " + item.getName());
            if (position == 0 && adapter.getHeaderCount() > 0) {
                adapter.removeAllHeaders();
            } else if (position == 1 && adapter.getHeaderCount() >= 2) {
                adapter.removeHeaderView(1);
            } else if (position == adapter.getDataSize() - 2 && adapter.getFooterCount() >= 2) {
                adapter.removeFooterView(1);
            } else if (position == adapter.getDataSize() - 1 && adapter.getFooterCount() > 0) {
                adapter.removeAllFooters();
            } else {
                if (adapter.notifyItemRemove(item)) {
                    adapter.showCallback(EmptyCallback.class, view -> {
                        adapter.showDefaultCallback();
                        show(3);
                    });
                }
            }
            *//*if (adapter.notifyRemoveItem(item)) {
                adapter.showCallback(EmptyCallback.class, view -> {
                    adapter.showDefaultCallback();
                    show(3);
                });
            }*//*
            Toast.makeText(this, "item->" + (position + 1), Toast.LENGTH_SHORT).show();
        });*/
        recyclerView.setAdapter(adapter);
        final View header = LayoutInflater.from(this).inflate(R.layout.header_rcycler_view_adapter_view, recyclerView, false);
        header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*if (adapter.notifyData(null)) {
                    adapter.showCallback(EmptyCallback.class, v -> {
                        adapter.showDefaultCallback();
                        show(3);
                    });
                }*/
                adapter.notifyItemMove(3,5);
//                adapter.removeHeaderView(header);
            }
        });
        final View footer = LayoutInflater.from(this).inflate(R.layout.footer_recycler_view_adapter_view, recyclerView, false);
        footer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.removeFooterView(footer);
            }
        });
        adapter.addHeaderView(header);
        final View header1 = LayoutInflater.from(this).inflate(R.layout.header_rcycler_view_adapter_view, recyclerView, false);
        final View header2 = LayoutInflater.from(this).inflate(R.layout.header_rcycler_view_adapter_view, recyclerView, false);
//        adapter.addHeaderView(header1);
//        adapter.addHeaderView(header2);
        adapter.addFooterView(footer);
        final View footer1 = LayoutInflater.from(this).inflate(R.layout.footer_recycler_view_adapter_view, recyclerView, false);
        final View footer2 = LayoutInflater.from(this).inflate(R.layout.footer_recycler_view_adapter_view, recyclerView, false);
        adapter.addFooterView(footer1);
        adapter.addFooterView(footer2);

        GridItemDecoration itemDecoration = new GridItemDecoration(this);
//        LinearItemDecoration itemDecoration = new LinearItemDecoration(this);
        itemDecoration.setDrawable(R.mipmap.ic_stcs_btm_line)
                .setMarginDrawable(R.mipmap.ic_stcs_btm_line)
                .setVerticalSize(10f)
                .setHorizontalSize(5f)
                .setHorizontalMarginSize(20f)
                .setVerticalMarginSize(30f)
//                .setColor("#ff0000")
//                .setSize(10f)
                .setItemDecorationListener(new ItemDecorationListener() {
                    @Override
                    public int getStart() {
                        return adapter.getHeaderCount();
                    }

                    @Override
                    public int getDrawCount() {
                        return adapter.getDataSize();
                    }
                });
        recyclerView.addItemDecoration(itemDecoration);
        ItemTouchHelper mItemHelper = new ItemTouchHelper(new ItemDragAndSwipeCallback(adapter));
        mItemHelper.attachToRecyclerView(recyclerView);
//        show(1);
//        adapter.notifyItemInsert(getData(11, 1));
        if (adapter.setNewData(null)) {
            adapter.showCallback(EmptyCallback.class, v -> {
//                adapter.removeEmptyCallback();
                adapter.showDefaultCallback();
                show(3);
            });
            adapter.notifyDataSetChanged();
        }
    }

    private void loadMore(int type) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (type == 1) {
                    adapter.loadFinish(getData(10, 3), true);
                } else if (type == 2) {
                    adapter.loadFail();
                } else if (type == 10) {
                    adapter.setNoData(true);
                    adapter.setNoDataShow(true);
                    List<Test> data = getData(14, 3);
//                    int dataSize = adapter.getDataSize();
//                    adapter.loadFinish();
//                    adapter.notifyAddAllItem(data);
//                    adapter.notifyLastRowChanged(dataSize);
                    adapter.loadFinish(data, true);
                } else {
                    adapter.notifyItemInsert(10, getData(14, 3));
                    adapter.loadFinish();
//                    adapter.loadFinish(, true);
                }
            }
        }, 3000);
    }

    private void show(final int type) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e("TAG", "run: " + type);
                if (type == 1) {
                    adapter.showCallback(ErrorCallback.class, view -> {
                        adapter.showDefaultCallback();
                        show(2);
                    });
                } else if (type == 2) {
                    adapter.showCallback(EmptyCallback.class, view -> {
                        adapter.showDefaultCallback();
                        show(3);
                    });
                } else {
                    adapter.notifyItemInsert(getData(15, 0));
                }
            }
        }, 3000);
    }

    public List<Test> getData(int index, int type) {
        List<Test> list = new ArrayList<>();
        for (int i = 0; i < index; i++) {
            if (type == 1) {
                list.add(new Test("刷新" + (i + 1)));
            } else if (type == 2) {
                list.add(new Test("重新加载" + (i + 1)));
            } else {
                list.add(new Test("加载" + (i + 1)));
            }
        }
        return list;
    }

    /**
     * 把dip转为px
     *
     * @param dip
     * @return
     */
    private int dip2px(int dip) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
    }
}
